﻿var MathUtil = {
    // Compute the distance between Cartesian points (x1,y1) and (x2,y2).
    getCordDistance: function (x1, y1, x2, y2) {
        var dx = x2 - x1;
        var dy = y2 - y1;

        return Math.sqrt(dx * dx + dy * dy);
    },

    // A recursive function (one that calls itself) that computes factorials
    // Recall that x! is the product of x and all positive integers less than it.
    factorial: function (x) {
        if (x <= 1) return 1;

        return x * arguments.callee(x - 1);
    },

    //changable arguments
    max: function () {
        var max = Number.NEGATIVE_INFINITY;
        for (var i = 0; i < arguments.length; i++) {
            if (arguments[i] > max)
                max = arguments[i];
        }

        return max;
    },

    min: function () {
        var min = Number.MAX_VALUE;
        for (var i = 0; i < arguments.length; i++) {
            if (arguments[i] < min)
                min = arguments[i];
        }

        return min;
    },

    gcd: function (a, b) {
        var t;
        if (a < b) t = b, b = a, a = t;
        while (b != 0) t = b, b = a % b, a = t;

        return a;
    },

    /*
        max common divisor
     */
    maxCommonDivisor: function (a, b) {
        var gcdmemo = CacheUtil.memorize(MathUtil.gcd);

        return gcdmemo(a, b);
    }
};

var Operator = {
    add: function (opLeft, opRight) {
        return opLeft + opRight;
    },

    subtract: function (opLeft, opRight) {
        return opLeft - opRight;
    },

    multiply: function (opLeft, opRight) {
        return opLeft * opRight;
    },

    divide: function (opLeft, opRight) {
        return opLeft / opRight;
    }
};

